package com.hubu.web;

import com.hubu.mapper.studentUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@WebServlet("/changeStudentPassword")
public class changeStudentPassword extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter writer = response.getWriter();
        //获取学生数据
        String id=request.getParameter("id");

        StringBuffer pass=new StringBuffer();
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie : cookies)
        {
            String password=cookie.getName();
            if("password".equals(password)){
                pass.append(cookie.getValue());
            }
        }
        String oldpassword = new String(pass);  //利用Cookie在找到登录的学生密码

        String inputoldpassword = request.getParameter("oldpassword");
        String password=request.getParameter("password");
        String password1=request.getParameter("password1");

        if(!password1.equals(password)) {
            writer.println("<script type='text/javascript'>");
            writer.println("alert('两次输入的密码不一样！');");
            writer.println("window.location='" + request.getContextPath() + "/changeStudentPassword.jsp';");
            writer.println("</script>");
        } else if (!inputoldpassword.equals(oldpassword)) {
            writer.println("<script type='text/javascript'>");
            writer.println("alert('原密码错误！');");
            writer.println("window.location='" + request.getContextPath() + "/changeStudentPassword.jsp';");
            writer.println("</script>");
        } else {
            //2. 调用Mybatis完成查询
            // 2.1 获取SqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            //2.2获取SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //2.3 获取Mapper
            studentUserMapper userMapper = sqlSession.getMapper(studentUserMapper.class);

            userMapper.updatePassword(id, password);
            //提交数据库
            sqlSession.commit();
            //释放资源

            writer.println("<script type='text/javascript'>");
            writer.println("alert('修改成功！');");
            writer.println("window.location='" + request.getContextPath() + "/studentHome';");
            writer.println("</script>");
            sqlSession.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
